import json

from framework.common.pt_logger import logger
from spider_module_loader import ModuleLoader
from spider_registry import worker_registry
from util import queueWrapper

overridden_parameters = {
    "enableNotification": False
}


def processRequest(messageList, event=None, context=None):
    logger.info("started")
    logger.info("messageList: %s" % messageList)
    for message in messageList:
        task_json = json.loads(message.get("messageBodyText"))
        spider_name = task_json["metadata"]["SpiderName"]
        module_name = worker_registry[spider_name]["WorkerName"]
        spider_module = ModuleLoader(module_name=module_name,
                                     spider_name=spider_name)
        spider_lib = spider_module.load()
        spider_lib.start_worker(task_json)


globals()['queueWrapper'].global_parameters = {**globals()['queueWrapper'].global_parameters, **overridden_parameters}
globals()['queueWrapper'].processRequest = processRequest
globals()['handler'] = queueWrapper.handler